package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ProductBean;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ProductServiceImpl implements IProductService {
    @Autowired
    private IProductMapper mapper;

    @Override
    public IPage<ProductBean> cutByItem(Integer pageNO, String pName, Integer startNum, Integer endNum, Integer sellPrice, String type) {
        QueryWrapper<ProductBean> qw = new QueryWrapper<>();
        if (pName != null && pName != "") {
            qw.like("p_pName", pName);
        }
        if (startNum != null) {
            qw.ge("p_stockNum", startNum);
        }
        if (endNum != null) {
            qw.le("p_stockNum", endNum);
        }
        if (sellPrice != null) {
            qw.eq("p_sellPrice", sellPrice);
        }
        if (type != null && type != "") {
            qw.like("p_type", type);
        }
        return mapper.selectPage(new Page<>(pageNO, 3), qw);
    }

    @Override
    public ProductBean findByProductId(String productId) {
        return mapper.selectById(productId);
    }

    @Override
    public void updateSellPrice(String productId, Integer newSellPrice) {
        ProductBean product = mapper.selectById(productId);
        product.setSellPrice(newSellPrice);
        mapper.updateById(product);
    }

    @Override
    public IPage<ProductBean> cutItem(Integer pageNO, String pName, String type) {
        QueryWrapper<ProductBean> qw = new QueryWrapper<>();
        if (pName != null && pName != "") {
            qw.like("p_pName", pName);
        }
        if (type != null && type != "") {
            qw.like("p_type", type);
        }
        qw.ge("p_stockNum", 1);
        qw.ge("p_sellPrice", 1);
        return mapper.selectPage(new Page<>(pageNO, 3), qw);
    }
}
